package com.letu.mq.util;

import com.letu.core.context.ContextUtil;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.UUID;

/**
 * 完善消息实体，完善message信息
 */
public class MessageKeyUtil {

    /**
     * 组装key
     * @return
     */
    public static String assembleKey() {
        if (StringUtils.isNotBlank(ContextUtil.getTrace())) {
            return ContextUtil.getTrace() + "-" + UUID.randomUUID().toString().replaceAll("-", "");
        }
        //唯一业务id
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 获取埋点名称
     * @param object
     * @return
     */
    public static String getPointNameInvok(Object object){
        Method m = null;
        try {
            m = object.getClass().getMethod("getPointObj");
            Object pointObj = m.invoke(object);
            if(pointObj==null){
                return null;
            }
            m = pointObj.getClass().getMethod("getPointName");
            Object pointName = m.invoke(pointObj);
            if(pointObj==null){
                return null;
            }
            return pointName.toString();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return null;
    }



    /**
     * 获取埋点名称
     * @param object
     * @return
     */
    public static Map<String,Object> getHeaderMapInvok(Object object){
        Method m = null;
        try {
            m = object.getClass().getMethod("getHeaderMap");
            Object headerMap = m.invoke(object);
            if(headerMap==null){
                return null;
            }
            Map<String,Object> result = (Map<String, Object>) headerMap;
            return result;
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return null;
    }


    /**
     * 获取排序orderid
     * @param object
     * @return
     */
    public static String getOrderIdInvok(Object object){
        Method m = null;
        try {
            m = object.getClass().getMethod("getPointObj");
            Object pointObj = m.invoke(object);
            if(pointObj==null){
                return null;
            }
            m = pointObj.getClass().getMethod("getOrderPointOrderId");
            Object orderId = m.invoke(pointObj);
            if(pointObj==null){
                return null;
            }
            return orderId.toString();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return null;
    }
}
